import { Component } from '@angular/core';
import { ActivatedRoute } from '@angular/router';
import { Subscription, ReplaySubject } from 'rxjs';
import { CompanyService } from '../company.service';
import { CompanyControversiesService } from './companyControversies.service';
import { ControversyQualitativePerformanceModel } from '../../../../shared/models/qualitativePerformance.model';
import { ControversyGroupModel } from './components/controversiesIncidents/models/controversyGroup.model';
import { CompanyModel } from '../company.model';
import { CompanyReportDownloadModel } from '../shared/companyHeader/companyReportDownload/companyReportDownload.model';
import { ReportAnalytics } from '../../../../shared/models/googleAnalytics.model';

/**
 * Company controversies main component
 *
 * Author: Sorin Spiru
 * Date: 13.01.2018
 */
@Component({
    moduleId: module.id,
    templateUrl: 'companyControversies.component.html',
    styleUrls: ['./companyControversies.component.scss'],
    providers: [CompanyControversiesService]
})
export class CompanyControversiesComponent {
    private sub: Subscription;
    private incidentsSub: Subscription;
    private performanceSubject: ReplaySubject<ControversyQualitativePerformanceModel[]>;
    private comparisonSubject: ReplaySubject<ControversyGroupModel[]>;
    private menuItems: any[] = [];
    public company: CompanyModel;
    public downloadData: CompanyReportDownloadModel;
    public analyticsData: ReportAnalytics = new ReportAnalytics('Research', 'PDFControversyReport');

    constructor(
        private route: ActivatedRoute,
        private companyService: CompanyService,
        private companyControversiesService: CompanyControversiesService        
    ) { }

    ngOnInit() {
        this.companyService.menuItems.next(this.route.routeConfig.children);

        this.sub = this.companyService.company.subscribe(company => {
            this.company = company;
            // set download data
            this.downloadData = this.companyService.getControversyDownloadData(this.company);
            // get incidents
            this.performanceSubject = this.companyControversiesService.getControversiesByTheme(company.id);
            // get comparisons
            this.comparisonSubject = this.companyControversiesService.getComparisonGroupControversies(company.id);
        });

        this.incidentsSub = this.companyControversiesService.controviersiesTheme.subscribe(data => {
            // add menu items
            ['Environment', 'Social', 'Governance'].forEach(theme => {
                data.filter(controversy => {
                    return controversy.themeName === theme;
                }).forEach(incident => {
                    this.menuItems.push({
                        title: incident.name,
                        path: ['controversies', 'incidents', incident.number]
                    });
                });
            });

            // update routes data
            let issueRoute = this.route.routeConfig.children.find(x => x.path == 'incidents/:number');
            issueRoute.data.menuItems = this.menuItems;

            // notify the companyMenu component to display sub menu items
            this.companyService.menuItems.next(this.route.routeConfig.children);
            issueRoute.data.menuItems = [];
        });
    }

    ngOnDestroy() {
        this.sub.unsubscribe();
        this.incidentsSub.unsubscribe();
        //this.performanceSubject.complete();// To refactor system of subscribe and broadcast
    }
}
